//
//  WNetDealTools.m
//  LWB_Project
//
//  Created by 王剑亮 on 15/11/12.
//  Copyright © 2015年 wangjianliang. All rights reserved.
//

#import "WNetDealTools.h"
#import "NewsModels.h"
#import "AFHTTPRequestOperationManager.h"
#import "DebugNSLog.h"
#import "NewsPicture.h"
#import "RadioModel.h"
@interface WNetDealTools ()

@end
static   AFHTTPRequestOperationManager *Mymanager ;

@implementation WNetDealTools


#pragma mark -- get method
#pragma mark--- 获取当前的网络状态
/**
 *  AFNetworkReachabilityStatusUnknown          = -1,  // 未知
 *  AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
 *  AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G
 *  AFNetworkReachabilityStatusReachableViaWiFi = 2,   // 局域网络Wifi
 */
+ (void)checkNetWorkStatus:(void (^)(AFNetworkReachabilityStatus status))block{

    // 如果要检测网络状态的变化, 必须要用检测管理器的单例startMoitoring
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // 检测网络连接的单例,网络变化时的回调方法
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        #pragma mark--- 回传回去网络的状态
        block( status );
        
        if(status == AFNetworkReachabilityStatusNotReachable){
            
            NSLog(@"网络连接已断开，请检查您的网络！");
            
            return ;
        }
    }];
    
}
//NewsTypeHeadlines,   //头条
//NewsTypeRecreation,  //娱乐
//NewsTypeSports,      //体育
//NewsTypeFinancial,   //财经
//NewsTypeTechnology,  //科技
//NewsTypeCar,         //汽车
//NewsTypeFashion,     //时尚
//NewsTypeMilitary,    //军事
//NewsTypeGame         //游戏
//1.第一个界面:新闻
//首页新闻接口:1⃣️头条:http://c.3g.163.com/nc/article/headline/T1295501906343/0-20.html
//2⃣️娱乐:http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html
//3⃣️:体育:http://c.3g.163.com/nc/article/list/T1348649079062/0-20.html
//4⃣️财经:http://c.3g.163.com/nc/article/list/T1348648756099/0-20.html
//5⃣️科技:http://c.3g.163.com/nc/article/list/T1348649580692/0-20.html
//6⃣️汽车:http://c.3g.163.com/nc/article/list/T1348654060988/0-20.html
//7⃣️时尚:http://c.3g.163.com/nc/article/list/T1348650593803/0-20.html
//8⃣️军事:http://c.3g.163.com/nc/article/list/T1348648141035/0-20.html
//9⃣️游戏:http://c.3g.163.com/nc/article/list/T1348654151579/0-20.html
//新闻详情界面接口:http://c.3g.163.com/nc/article/%@/full.html
//通过上一界面传来的docIid进行拼接

#pragma mark--- 输入新闻类型 和你想要的页数每页20个Model block回调 返回新闻类型的 model 数组
+(void)GetNewsModelNewType:(NewsType )type Pages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{

    switch (type)
    {
        case NewsTypeHeadlines: [self dealHeadlinesPages:Pages block:block];
            break;
        case NewsTypeRecreation: [self dealRecreationsPages:Pages block:block];
            break;
        case NewsTypeSports: [self dealSportsPages:Pages block:block];
            break;
        case NewsTypeFinancial: [self dealFinancialPages:Pages block:block];
            break;
        case NewsTypeTechnology: [self dealTechnologyPages:Pages block:block];
            break;
        case NewsTypeCar: [self dealCarPages:Pages block:block];
            break;
        case NewsTypeFashion: [self dealFashionPages:Pages block:block];
            break;
        case NewsTypeMilitary: [self dealMilitaryPages:Pages block:block];
            break;
        case NewsTypeGame: [self dealGamePages:Pages block:block];
            break;
            
        default:
            break;
    }

}
#pragma mark--- 解析网络下载的字典数据
+(NSArray *)DealNetLoadDic:(NSDictionary *)dic
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    
    //解析字典变成Model数组
    NSArray *Array = (NSArray *)[dic allValues][0];
    MyLog(@"%@",Array);
    //解析成model
    for (NSDictionary *dic in Array)
    {
        NewsModels *model = [[NewsModels alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        #pragma mark--- 添加到数据model数组
        [arr addObject:model];
    }

    return arr;
}
#pragma mark--- 处理头条的解析
+(void)dealHeadlinesPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    
    
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/headline/T1295501906343/%ld-%ld.html",Pages*20,(Pages+1)*20];
    Mymanager = [AFHTTPRequestOperationManager manager];
    
    Mymanager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    
    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         //MyLog(@"JSON: %@", responseObject);
         #pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理娱乐的解析
+(void)dealRecreationsPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348648517839/%ld-%ld.html",Pages*20,(Pages+1)*20];

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
#pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理体育的解析
+(void)dealSportsPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348649079062/%ld-%ld.html",Pages*20,(Pages+1)*20];

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
#pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理财经的解析
+(void)dealFinancialPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348648756099/%ld-%ld.html",Pages*20,(Pages+1)*20];

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
#pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理科技的解析
+(void)dealTechnologyPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348649580692/%ld-%ld.html",Pages*20,(Pages+1)*20];

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
#pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理汽车的解析
+(void)dealCarPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348654060988/%ld-%ld.html",Pages*20,(Pages+1)*20];

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
         #pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理时尚的解析
+(void)dealFashionPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348650593803/%ld-%ld.html",Pages*20,(Pages+1)*20];

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
         #pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}
#pragma mark--- 处理军事的解析
+(void)dealMilitaryPages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348648141035/%ld-%ld.html",Pages*20,(Pages+1)*20];

    
    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
#pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}

#pragma mark--- 处理游戏的解析
+(void)dealGamePages:(NSInteger )Pages block:(void (^)(NSArray *ModelArray) )block
{
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/T1348654151579/%ld-%ld.html",Pages*20,(Pages+1)*20];
  
    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
          #pragma mark--- 解析成model数组回传回去
         block([WNetDealTools DealNetLoadDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}


#pragma mark--- 处理cell点击的图片那个数据 给我一个那个model.photosetID
+(void)dealCellModelPhotosetID:(NSString * )str block:(void (^)(NSArray *ModelArray) )block
{
        NSMutableArray *array = [[NSMutableArray alloc] init];
   #pragma mark--- 分析数据
    //单独是图片的数据
    NSArray * arraystr = [str componentsSeparatedByString:@"|"];
    NSString * strr = [arraystr.firstObject substringFromIndex:4];
   #pragma mark--- 拼接数据信息
    strr = [NSString stringWithFormat:@"%@/%@",strr,arraystr.lastObject];
    NSString *string = [NSString stringWithFormat:@"http://c.m.163.com/photo/api/set/%@.json",strr];
    NSLog(@"%@",string);
    

    [Mymanager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
         
         NSDictionary *dic = responseObject;
         
         NSArray *arr = [dic objectForKey:@"photos"];
         
                  //解析model
         for (NSDictionary *dic1 in arr) {
             NewsPicture *model = [[NewsPicture alloc] init];
             
             NSString *imageStr = [dic1 objectForKey:@"imgurl"];
             
             [model setValue:imageStr forKey:@"imageUrl"];
             [array addObject:model];
         }

         block(array);

     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];
}

//3⃣️
#pragma mark---解析电台数据的方法
+(void)dealRadioModelBlock:(void (^)(NSArray *ModelArray) )block
{
    //网址
    NSString *urlStr = [NSString stringWithFormat:@"http://app.9nali.com/index/876?page_id=1&device=iPhone&version=1.3.2"];
    
    [Mymanager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         MyLog(@"JSON: %@", responseObject);
#pragma mark--- 解析成model数组回传回去
         block([WNetDealTools dealRadioDic:responseObject]);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {     NSLog(@"Error: %@", error);
     }];

}

//2⃣️
+ (NSArray *)dealRadioDic:(NSDictionary *)dic
{
    NSMutableArray *radioArray = [NSMutableArray array];
    
    NSArray *arr1 = dic[@"list"];
    
    for (NSDictionary *dic1 in arr1) {
        
        RadioModel *model = [[RadioModel alloc] init];
        
        [model setValuesForKeysWithDictionary:dic1];
        
        [radioArray addObject:model];
    }

    return radioArray;
}








@end
